package com.hainatong.mvvmbase.utils;

import java.util.Locale;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.LocaleList;
import android.util.DisplayMetrics;
import android.util.Log;

/**
 * 系统语言 的Locale配置
 *
 * Created By j on 2019/7/12
 */
public class LocaleManageUtil
{
    private static final String TAG = "LocaleManageUtil";

    public static final int LOCAL_CHINA = 0;
    public static final int LOCAL_ENGLISH = 1;

    public static final int LOCAL_FAYU = 2;
    public static final int LOCAL_MALAIYU = 3;
    public static final int LOCAL_YDUYU = 4;
    public static final int LOCAL_TAIYU = 5;

    /**
     * 获取系统的locale
     *
     * @return Locale对象
     */
    public static Locale getSystemLocale(Context context)
    {
        // 默认 系统 语言
        return SharePreferenceUtil.getInstance(context).getSystemCurrentLocal();
    }


    /**
     * 设置系统的Locale
     *
     * @param context
     * @return
     */
    public static Context setLocal(Context context , int LocalType)
    {
        if( LocalType == LOCAL_CHINA)
        {
            return  updateResources(context, Locale.CHINA);
        }
        else if( LocalType == LOCAL_ENGLISH )
        {
            return  updateResources(context, Locale.ENGLISH);
        }
        else
        {
            return updateResources(context, getSetLanguageLocale(context));
        }
    }

    /**
     *
     * @param context
     * @param select  0跟随系统  1简体中文  2English      3
     */
    public static void saveSelectLanguage(Context context, int select)
    {
        SharePreferenceUtil.getInstance(context).saveLanguage(select);
        setApplicationLanguage(context);
    }

    /**
     * 设置系统的Locale
     *
     * @param context
     * @return
     */
    public static Context setLocal(Context context)
    {
        return updateResources(context, getSetLanguageLocale(context));
    }

    private static Context updateResources(Context context, Locale locale)
    {
        Locale.setDefault(locale);

        Resources res = context.getResources();
        Configuration config = new Configuration(res.getConfiguration());
        if (Build.VERSION.SDK_INT >= 17) {
            config.setLocale(locale);
            context = context.createConfigurationContext(config);
        } else {
            config.locale = locale;
            res.updateConfiguration(config, res.getDisplayMetrics());
        }
        return context;
    }

    /**
     * 获取选择的语言设置Locale
     *
     * @param context
     * @return
     */
    public static Locale getSetLanguageLocale(Context context)
    {
        switch (SharePreferenceUtil.getInstance(context).getSelectLanguage())
        {
            case 0:
                //系统语言
                return getSystemLocale(context);
            case 1:
                //中文
                return Locale.CHINA;
            /*
            case 2:
                return Locale.TAIWAN;
            */
            case 2:
                return Locale.ENGLISH;
            default:
                if (getSystemLocale(context).getLanguage().contains("zh") ||
                        getSystemLocale(context).getCountry().contains("CN"))
                {
                    return Locale.CHINA;
                }
                else return Locale.ENGLISH;

        }
    }


    public static String getSelectLanguage(Context context)
    {
           /*
            <string name="language_cn">中文</string>
            <string name="language_en">ENGLISH</string>
            <string name="language_auto">自動</string>
            <string name="language_traditional">繁体</string>
            */
        switch (SharePreferenceUtil.getInstance(context).getSelectLanguage())
        {
            case 0:
                //context.getString(R.string.language_auto)
                return "自動";
            case 1:
                //context.getString(R.string.language_cn)
                return "中文";
            case 2:
                //context.getString(R.string.language_en)
                return "ENGLISH";
            //case 3:return context.getString(R.string.language_traditional);
            default:
                //context.getString(R.string.language_cn)
                return "中文";
        }
    }

    /**
     * 保存系统选择语言
     * @param context
     */
    public static void saveSystemCurrentLanguage(Context context)
    {
        Locale locale;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
        {
            locale = LocaleList.getDefault().get(0);
        }
        else
        {
            locale = Locale.getDefault();
        }
        Log.d(TAG, locale.getLanguage());
        SharePreferenceUtil.getInstance(context).setSystemCurrentLocal(locale);
    }

    public static void onConfigurationChanged(Context context)
    {
        saveSystemCurrentLanguage(context);
        setLocal(context);
        setApplicationLanguage(context);
    }


    /**
     * 设置语言类型
     */
    public static void setApplicationLanguage(Context context)
    {
        Resources resources = context.getApplicationContext().getResources();
        DisplayMetrics dm = resources.getDisplayMetrics();
        Configuration config = resources.getConfiguration();
        Locale locale = getSetLanguageLocale(context);
        config.locale = locale;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
        {
            LocaleList localeList = new LocaleList(locale);
            LocaleList.setDefault(localeList);
            config.setLocales(localeList);
            context.getApplicationContext().createConfigurationContext(config);
            Locale.setDefault(locale);
        }
        resources.updateConfiguration(config, dm);
    }

}
